package de.barny.swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;

import javax.swing.AbstractButton;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;

import de.barny.prefs.Var;

public class GameButton extends BasicButtonUI implements MouseListener, KeyListener {

	private final static GameButton m_buttonUI = new GameButton();

	Color buttonBg = Var.initFrameBackgroundColor.brighter();

	protected Dimension dim = new Dimension(Var.windowWidth/4, 80);

	public static ComponentUI createUI(JComponent c) {
		return m_buttonUI;
	}

	public void installUI(JComponent c) {
		super.installUI(c);
		c.setBackground(null);
		c.setForeground(buttonBg.darker().darker().darker());
		c.addMouseListener(this);
	}

	public void uninstallUI(JComponent c) {
		super.uninstallUI(c);
		c.removeMouseListener(this);
	}

	public void paint(Graphics gButton, JComponent c) {
		AbstractButton b = (AbstractButton) c;
		String caption = b.getText();
		Graphics2D button = (Graphics2D)gButton;
		button.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		Font font = null;
		File fontfile = new File("././res/font.ttf");
		try {
			font = Font.createFont(Font.TRUETYPE_FONT, fontfile);
		} catch (FontFormatException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		ge.registerFont(font);

		Font newFont = new Font(font.getName(), Font.PLAIN, Toolkit.getDefaultToolkit().getScreenSize().height/30);
		button.setFont(newFont);
		
		FontMetrics fm = button.getFontMetrics();
		dim.setSize(fm.stringWidth(caption), fm.getHeight());

		button.setColor(c.getForeground());
		button.drawString(caption, 0, fm.getAscent());
	}

	public Dimension getPreferredSize(JComponent c) {
		return dim;
	}

	public void mouseClicked(MouseEvent e) {

	}

	public void mousePressed(MouseEvent e) {
		JComponent c = (JComponent) e.getComponent();
		c.setForeground(buttonBg.brighter().brighter());
		c.repaint();
	}

	public void mouseReleased(MouseEvent e) {
		JComponent c = (JComponent) e.getComponent();
		c.setForeground(buttonBg.darker().darker().darker().darker());
		c.repaint();
	}

	public void mouseEntered(MouseEvent e) {
		JComponent c = (JComponent) e.getComponent();
		c.setForeground(buttonBg.darker().darker().darker().darker());
		c.repaint();
	}

	public void mouseExited(MouseEvent e) {
		JComponent c = (JComponent) e.getComponent();
		c.setForeground(buttonBg.darker().darker().darker());
		c.repaint();
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}
}
